<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class invest_controller extends admin_controller
{
    public $category_id = null;

    // Get list
    public function __construct()
    {
        parent::__construct();
        $this->page_title = 'Đầu tư';
        $this->table_name = 'invest';
        $this->route_name = 'invest';
        $this->business_model = $this->invest_model;
    }

    public function index($category_id)
    {
        $this->category_id = $category_id;
        $header_data = array();
        $header_data['show_menu'] = true;
        $view_data = $this->get_view_data();
        $view_data['list'] = $this->get_list();
        $view_data['actions'] = $this->get_list_actions();
        $header_data['base_admin_url'] = $this->base_admin_url;
        $this->load->view('admin/master_page/header',$header_data);
        $this->load->view('admin/list_template',$view_data);
        $this->load->view('admin/master_page/footer');
    }

    public function get_list_actions()
    {
        $actions = array();
        $actions[] = array(
            'text' => 'Thêm',
            'href' => base_url().'ci-admin/'.$this->route_name.'/insert/'.$this->category_id,
            'view_change_method' => 'redirect'
        );
        $actions[] = array(
            'text' => 'Xóa',
            'href' => base_url().'ci-admin/'.$this->route_name.'/delete/'.$this->category_id,
            'view_change_method' => 'submit'
        );
        $actions[] = array(
            'text' => 'Quay về danh sách album hình',
            'href' => base_url().'ci-admin/category',
            'view_change_method' => 'redirect'
        );
        return $actions;
    }

    public function get_form_actions($id)
    {
        $actions = array();
        $actions[] = array(
            'text' => 'Lưu',
            'href' => base_url().'ci-admin/'.$this->route_name.'/save/'.$id,
            'view_change_method' => 'submit'
        );
        $actions[] = array(
            'text' => 'Hủy',
            'href' => base_url().'ci-admin/'.$this->route_name.'/'.$this->category_id,
            'view_change_method' => 'redirect'
        );
        return $actions;


    }

    public function update($id)
    {
        $invest = $this->invest_model->get_element_by_id($id);
        $this->category_id = isset($invest->category_id) ? $invest->category_id : 0;

        $header_data = array();
        $header_data['show_menu'] = true;
        $view_data = $this->get_view_data();
        $view_data['fields'] = $this->get_fields($id);
        $view_data['actions'] = $this->get_form_actions($id);
        $header_data['base_admin_url'] = $this->base_admin_url;
        $this->load->view('admin/master_page/header',$header_data);
        $this->load->view('admin/form_template',$view_data);
        $this->load->view('admin/master_page/footer');
    }

    public function insert($category_id)
    {

        $this->category_id = $category_id;
        $header_data = array();

        $header_data['show_menu'] = true;
        $view_data = $this->get_view_data();
        $view_data['fields'] = $this->get_fields(-1);
        $view_data['actions'] = $this->get_form_actions(-1);
        $header_data['base_admin_url'] = $this->base_admin_url;
        $this->load->view('admin/master_page/header',$header_data);
        $this->load->view('admin/form_template',$view_data);
        $this->load->view('admin/master_page/footer');
    }

    public function save($id)
    {
        $post_data = $this->get_post_data();
        if($id == -1)
        {
            $this->db->insert($this->table_name,$post_data);
            $id = $this->db->insert_id();
        }
        else
        {
            $this->db->update($this->table_name,$post_data,array('id' => $id));
        }
        $this->session->set_userdata('message',$this->save_notification);
        $music = $this->invest_model->get_element_by_id($id);
        redirect(base_url().'ci-admin/'.$this->route_name.'/'.$music->category_id);
    }




    public function get_list()
    {
        $list = array();
        $items = $this->business_model->get_elements_by_category($this->category_id);
        foreach($items as $item)
        {
            $actions = array();
            $actions[] = array(
                'text' => 'Sửa',
                'href' => base_url().'ci-admin/'.$this->route_name.'/update/'.$item->id
            );
            $category = $this->invest_model->get_element_by_id($item->category_id);
            $list[] = array(
                'can_delete'=>$item->id,
                'category_id'=>$this->category_model->get_element_by_id($item->category_id)->name,
                'company_logo' =>"<img src=".base_url().$item->company_logo." width='100' height='100' />",
                'company_name_en' => $item->company_name_en,
                'title_en' => isset($item->title_en)?$item->title_en :'',
                'description_en' => $item->description_en,
                'company_url' => $item->company_url,
                'actions' => $actions
            );
        }
        return $list;
    }

    public function get_header_list()
    {

        $list = array();
        $list[] = 'Loại';
        $list[] = 'Logo công ty';
        $list[] = 'Tên Công ty';
        $list[] = 'Tiêu đề';
        $list[] = 'Mô tả';
        $list[] = 'Đường dẫn website';
        $list[] = 'Hành động';
        return $list;
    }

    public function get_post_data()
       {
           $image = $this->upload_image('new_image',$this->input->post('image'),'uploads/logo/');
           $post_data = array(
               'category_id' => $this->input->post('category_id'),
               'company_name_en' => $this->input->post('company_name_en'),
               'company_name_vi' => $this->input->post('company_name_vi'),
               'title_en' => $this->input->post('title_en'),
               'title_vi' => $this->input->post('title_vi'),
               'description_en' => $this->input->post('description_en'),
               'description_vi' => $this->input->post('description_vi'),
               'company_url' => $this->input->post('company_url'),
               'company_logo' => $image,
               'modified_date'=> time()
           );
           return $post_data;
       }


    public function get_fields($id)
    {

        $model = new stdClass();

        if($id == -1)
        {
            $model->category_id = $this->category_id;
            $model->company_name = '';
            $model->title = '';
            $model->description = '';
            $model->company_url ='';
            $model->company_logo='';
            $model->modified_date = time();
        }
        else
        {
            $model = $this->business_model->get_element_by_id($id);
        }

        $fields = array();

        $fields[] = array(
            'text' => 'Tên công ty (Anh)',
            'name' => 'company_name_en',
            'value' => $model->company_name_en,
            'type' => 'text'
        );
        $fields[] = array(
            'text' => 'Tên công ty(Việt)',
            'name' => 'company_name_vi',
            'value' => $model->company_name_vi,
            'type' => 'text'
        );

        $fields[] = array(
            'text' => 'Tiêu dề phụ(option)(Anh)',
            'name' => 'title_en',
            'value' => $model->title_en,
            'type' => 'text'
        );
        $fields[] = array(
            'text' => 'Tiêu dề phụ(option)(Việt)',
            'name' => 'title_vi',
            'value' => $model->title_vi,
            'type' => 'text'
        );

        $fields[] = array(
            'text' => 'Mô tả về công ty(Anh)',
            'name' => 'description_en',
            'value' => $model->description_en,
            'type' => 'textarea'
        );
        $fields[] = array(
            'text' => 'Mô tả về công ty(Việt)',
            'name' => 'description_vi',
            'value' => $model->description_vi,
            'type' => 'textarea'
        );

        $fields[] = array(
                   'text' => 'Đường dẫn website công ty'.'<br/> Exp: http://www.yoursite.com',
                   'name' => 'company_url',
                   'value' => $model->company_url,
                   'type' => 'text'
               );



        $fields[] = array(
                    'text' => 'Logo công ty',
                    'name' => '',
                    'value' => $model->company_logo != null ? $model->company_logo: '',
                    'type' => 'image'
                );


                $fields[] = array(
                    'text' => 'Đường dẫn poster',
                    'name' => 'image',
                    'value' => $model->company_logo,
                    'type' => 'hidden'
                );

                $fields[] = array(
                    'text' => 'Tải poster khác',
                    'name' => 'new_image',
                    'value' => '',
                    'type' => 'file'
                );


        $fields[] = array(
            'text' => 'Phân loại',
            'name' => 'category_id',
            'value' => $model->category_id,
            'type' => 'select',
            'options' => $this->category_model->get_elements_invest(),
            'select_display' => 'name_en',
            'select_value' => 'id'

        );



        return $fields;
    }
}